package com.aistar.controller;

import com.aistar.model.dto.job.RecruitmentAndCompanyDTO;
import com.aistar.model.entity.*;
import com.aistar.service.job.*;
import com.aistar.service.job.ITagService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: JobController
 * @author: LiuWenXiu
 * @date: 2021/9/24 17:36
 * @Description: TODO...本类用来演示:
 */
@Controller
public class JobController {
    @Autowired
    private IRecruitmentService recruitmentService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IRecruitmentLabelService recruitmentLabelService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private IEmployeeCollectionService employeeCollectionService;
    @Autowired
    private IEmployeeBlackListService employeeBlackListService;
    @Autowired
    private IDeliveryService deliveryService;


    @GetMapping("/job/company/company")
    public String company(HttpServletRequest request, String name, String financing, String num, String province, String city, String district, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "16") Integer limit) {
        if (financing != null && financing.length() == 0 || num != null && num.length() == 0 || city != null && city.length() == 0) {
            financing = null;
            num = null;
            city = null;
        }
        PageInfo<Company> company = companyService.cutPage(name, province, city, district, financing, num, page, limit);
        request.setAttribute("company", company);
        request.setAttribute("name", name);
        request.setAttribute("province", province);
        request.setAttribute("city", city);
        request.setAttribute("district", district);
        request.getParameter("id");
        return "/WEB-INF/job/company/company.jsp";
    }

    @PostMapping("/job/company/companyPage")
    public String companyPage(HttpServletRequest request, Integer id, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer limit) {
        List<Recruitment> recruitment = recruitmentService.searchByCompanyId(id);
        request.setAttribute("recruitment", recruitment);
        PageInfo<Recruitment> pageInfo = recruitmentService.searchCidAndPage(id, page, limit);
        request.setAttribute("info", pageInfo);
        return "/WEB-INF/job/page/companyPage.jsp";
    }

    @GetMapping("/job/company/companyinfo")
    public String company_info(HttpServletRequest request, Integer id) {
        List<Recruitment> recruitment = recruitmentService.searchByCompanyId(id);
        request.setAttribute("recruitment", recruitment);
        Integer count = recruitmentService.searchRecruitment(id);
        request.setAttribute("count", count);
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        Integer r1 = recruitmentService.countEmployer(id);
        request.setAttribute("r1", r1);
        return "/WEB-INF/job/company/companyInfo.jsp";
    }

    @GetMapping("/job/recruitment/recruitment")
    public String recruitment(HttpServletRequest request, String name, String experience, String education, @RequestParam(defaultValue = "0") Integer salaryLower, @RequestParam(defaultValue = "20") Integer salaryUpper, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String tagdt, String province, String city, String district, Integer tagId) {
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = new ArrayList<>();
        if (experience != null && experience.length() == 0 || education != null && education.length() == 0) {
            experience = null;
            education = null;
        }
        PageInfo<Recruitment> cutPage = recruitmentService.cutPage(name, experience, education, salaryLower, salaryUpper, province, city, district, page, limit, tagId);
        if (null != cutPage || cutPage.getList().size() > 0) {
            for (Recruitment recruitment : cutPage.getList()) {
                RecruitmentAndCompanyDTO recruitmentAndCompanyDTO = new RecruitmentAndCompanyDTO();
                recruitmentAndCompanyDTO.setRecruitment(recruitment);
                System.out.println(recruitment);
                Company company = companyService.findById(recruitment.getCompanyId());
                System.out.println(company);
                recruitmentAndCompanyDTO.setCompany(company);
                List<Integer> tId = recruitmentLabelService.getTag(recruitment.getId());
                List<String> tagList = new ArrayList<>();
                for (Integer integer : tId) {
                    String tag = tagService.searchDetails(integer);
                    tagList.add(tag);
                    recruitmentAndCompanyDTO.setTagList(tagList);
                }
                recruitmentAndCompanyDTOList.add(recruitmentAndCompanyDTO);
            }
        }
        request.setAttribute("recruitmentCompany", recruitmentAndCompanyDTOList);
        request.setAttribute("recruitment", cutPage);
        request.setAttribute("name", name);
        request.setAttribute("salaryLower", salaryLower);
        request.setAttribute("salaryUpper", salaryUpper);
        request.setAttribute("province", province);
        request.setAttribute("city", city);
        request.setAttribute("district", district);
        return "/WEB-INF/job/recruitment/recruitment.jsp";
    }

    @PostMapping("/job/recruitment/recruitmentbutton")
    public String recruitmentButton(HttpServletRequest request, Integer id) {
        // 获取session的求职者信息
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        // 根据职位id和求职者id判断是否收藏
        boolean isCol = employeeCollectionService.judgeCollection(employee.getId(),id);
        request.setAttribute("isCol",isCol);

        // 根据职位id和求职者id判断是否屏蔽
        boolean isBlack = employeeBlackListService.judgeBlacklist(employee.getId(),id);
        request.setAttribute("isBlack",isBlack);

        // 根据求职者id和职位id判断是否可以投递
        boolean isDelivery = deliveryService.judgeDelivery(id, employee.getId());
        request.setAttribute("isDelivery", isDelivery);

        // 加载职位信息
        Recruitment recruitment = recruitmentService.loadRecruitment(id);
        request.setAttribute("recruitment", recruitment);
        // 加载公司信息
        Company company = companyService.findById(recruitment.getCompanyId());
        request.setAttribute("company", company);

//        int count = employeeCollectionService.searchCount(id, employee.getId());
//        request.setAttribute("count", count);
//        int count2 = employeeBlackListService.searchCompany(employee.getId(), company.getId());
//        request.setAttribute("count2", count2);
        // 表示是否可以投递的标志

        return "/WEB-INF/job/recruitment/recruitmentButton.jsp";
    }

    @GetMapping("/job/recruitment/recruitmentinfo")
    public String recruitment_info(HttpServletRequest request, Integer id) {
        // 获取session中的求职者信息
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        // 根据职位id获取职位信息
        Recruitment recruitment = recruitmentService.loadRecruitment(id);
        // 根据公司id获取公司信息
        Company company = companyService.findById(recruitment.getCompanyId());
        // 根据求职者获取简历信息
        List<Resume> resumeList = recruitmentService.findByRecruitmentId(employee.getId());
        // 获取薪资福利
        List<String> welfare = recruitmentService.findWelfareByRecruitmentId(id);
        // 获取标签内容
        List<String> tagList = tagService.findTagByRecruitmentId(id);
        // 把获取的信息存储到request作用域中
        request.setAttribute("recruitment", recruitment);
        request.setAttribute("company", company);
        request.setAttribute("welfare", welfare);
        request.setAttribute("resume", resumeList);
        request.setAttribute("tag", tagList);


//        String[] welfare = recruitmentService.searchWelfare(id).split(",");
        //        List<Integer> tagId = recruitmentLabelService.getTag(recruitment.getId());
//        List<String> tagList = new ArrayList<>();
//        for (Integer integer : tagId) {
//            String tag = tagService.searchDetails(integer);
//            tagList.add(tag);
//        }
//        int flag = 0;
//        EmployeeCollection employeeCollection = employeeCollectionService.searchByRecruitmentId(id, employee.getId());
//        if (employeeCollection != null) {
//            flag = 1;
//        }
//        request.setAttribute("flag", flag);
        return "/WEB-INF/job/recruitment/recruitmentInfo.jsp";
    }


}
